# Contributor: Melven Roehrig-Zoellner <Melven.Roehrig-Zoellner@DLR.de>

_realname=tixi3
_name=tixi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.1
pkgrel=2
pkgdesc="TiXI is a fast and simple XML interface library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/DLR-SC/tixi'
license=('Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt")
source=("${_name}-${pkgver}.tar.gz::https://github.com/DLR-SC/tixi/archive/v${pkgver}.tar.gz"
        0001-Fixed-fortran-compilation-if-cflags-were-set-before.patch
        0002-Dont-build-and-install-the-examples.patch
        0003-Fix-ctypes-DLL-loading-in-Python-3.8-new-policy.patch)
sha256sums=('b42a9dd8599e7558625548764c6c324c2d862d537babbbe8540455288539ae32'
            '678a3df4f449c753faaf115b447688b1e3e24bb405b1d86d9622330085666f92'
            '632b435c9793f14c1f21509e8f9724eb0c3b17f5970b18f7c005c02b5160e502'
            '9becdc0571fa8c3dddcf6f2efd26a592d5d866144c0dc2b0130e1163b5b80d89')


prepare() {
  cd "${srcdir}/${_name}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-Fixed-fortran-compilation-if-cflags-were-set-before.patch"
  patch -Np1 -i "${srcdir}/0002-Dont-build-and-install-the-examples.patch"
  patch -Np1 -i "${srcdir}/0003-Fix-ctypes-DLL-loading-in-Python-3.8-new-policy.patch"
}


build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DTIXI_ENABLE_FORTRAN=On \
      -DBUILD_SHARED_LIBS=On \
      "${extra_config[@]}" \
      ../${_name}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}


package() {
  cd "${srcdir}"/build-${CARCH}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  install -Dm644 "${srcdir}/${_name}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
